extern "C"
{
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
}
#include "toluaclass.h"
extern int luaopen_toluaclass(lua_State* tolua_S);

CDerived1* toDerived1(void *p)
{
	return dynamic_cast<CDerived1*>((CBase*)p);
}

CDerived2& toDerived2(void *p)
{
	return *dynamic_cast<CDerived2*>((CBase*)p);
}

int main()
{
	lua_State* L = luaL_newstate();
	luaopen_base(L);
	luaopen_toluaclass(L);

	CBase *p1 = new CDerived1();
	CBase *p2 = new CDerived2();

	luaL_dofile(L, "zh.lua");

	// call Derived1Test
	lua_getglobal(L, "Derived1Test");
	lua_pushlightuserdata(L, p1);	
	if (lua_pcall(L, 1, 0, 0) != 0)
	{
		fprintf(stderr, "Call Derived1Test failed:%s", lua_tostring(L, -1));
	}

	// call Derived2Test
	lua_getglobal(L, "Derived2Test");
	lua_pushlightuserdata(L, p2);
	if (lua_pcall(L, 1, 0, 0) != 0)
	{
		fprintf(stderr, "Call Derived2Test failed:%s", lua_tostring(L, -1));
	}

	delete p1;
	delete p2;

	lua_close(L);
	return 0;
}